<?php
class Page extends Controller {

    function __construct() {
        parent::__construct();
    }
    function index() {
		$sql=new sql();
		$sql->connect();
		
		authentication();
		
		$sql=new sql();
		$sql->connect();
                $getPage=getPage();
		$this->view->getPage=$getPage;
		$dateTime=DateTime();
		$this->view->category=$sql->fetchAll("category",array("parent"=>'0'));	
		$this->view->template=$sql->fetchAll("template",array("1"=>'1'));		
    	
		if(isset($_POST['delete'])){
			for($i=0;$i<count($_POST['uid']);$i++)
			{
				$delete=$sql->delete('page',array("uid"=>$_POST['uid'][$i]));
			}
			if($delete)
			{
				header("Location: ".URL."page/?action=view&reply=PageDeleted");
			}
			else
			{
				header("Location: ".URL."page/?action=view&reply=PageError");
			}
		}
		
		
		if(isset($_POST['submit']) and isset($_POST['editUid'])){
			
			$arraydata=array("title"=>$_POST['title'],
								"heading"=>$_POST['heading'],
								"content"=>$_POST['content'],
								"category"=>$_POST['category'],
								"subCategory"=>$_POST['subCategory'],
								"template"=>$_POST['template'],
								"keywords"=>$_POST['keywords'],
								"description"=>$_POST['description'],
								"crawl"=>$_POST['crawl'],
								"optional1"=>$_POST['optional1'],
								"optional2"=>$_POST['optional2'],
								"optional3"=>$_POST['optional3'],
								"searchTags"=>$_POST['searchTags'],
								"customJS"=>$_POST['customJS'],
								"customCSS"=>$_POST['customCSS'],
								"LastModify"=>$dateTime['date_with_time'],
								"LastModifyBy"=>base64_decode($_COOKIE['login_user_uid']),
								"permalink"=>$_POST['permalink'],
								);
			//$permalink=permalink($_POST['editUid']);
			
			//$update=$sql->update('page',array("permalink"=>$permalink),array("uid"=>$_POST['editUid']));
				if($_FILES['featuredImage']['name']){
					$random_digit=time();
					$file_name =$_FILES['featuredImage']['name'];
					$new_file_name=$random_digit.$file_name;
					move_uploaded_file($_FILES["featuredImage"]["tmp_name"],BASEURL.IMAGES.$new_file_name);
					$fileLocation= IMAGES.$new_file_name;
					$arraydata["featuredImage"]=$fileLocation;
				}
				
				$update=$sql->update('page',$arraydata,array("uid"=>$_POST['editUid']));
				if($update)
				{
					header("Location: ".URL."page/?action=view&reply=PageUpdated");
				}
				else
				{
					header("Location: ".URL."page/?action=view&reply=PageError");
				}
		}
		elseif(isset($_POST['submit'])){
			$random_digit=time();
			if($_FILES['featuredImage']['name']){
				$file_name =$_FILES['featuredImage']['name'];
				$new_file_name=$random_digit.$file_name;
				move_uploaded_file($_FILES["featuredImage"]["tmp_name"],BASEURL.IMAGES.$new_file_name);
				$fileLocation= IMAGES.$new_file_name;
			}
			else
			{
				$fileLocatiom="";
			}
			$arraydata=array("title"=>$_POST['title'],
							"heading"=>$_POST['heading'],
							"content"=>$_POST['content'],
							"category"=>$_POST['category'],
							"subCategory"=>$_POST['subCategory'],
							"template"=>$_POST['template'],
							"keywords"=>$_POST['keywords'],
							"description"=>$_POST['description'],
							"crawl"=>$_POST['crawl'],
							"optional1"=>$_POST['optional1'],
							"optional2"=>$_POST['optional2'],
							"optional3"=>$_POST['optional3'],
							"searchTags"=>$_POST['searchTags'],
							"customJS"=>$_POST['customJS'],
							"customCSS"=>$_POST['customCSS'],
							"LastModify"=>$dateTime['date_with_time'],
							"LastModifyBy"=>base64_decode($_COOKIE['login_user_uid']),
							"featuredImage"=>$fileLocation,
							);
			$insert=$sql->insert('page',$arraydata);
			$last_id=$sql->last_id();
			$permalink=permalink($last_id);
			
			$update=$sql->update('page',array("permalink"=>$permalink),array("uid"=>$last_id));
			
			if($insert and $update)
			{
				header("Location: ".URL."page/?action=view&reply=PageCreated");
			}
			else
			{
				header("Location: ".URL."page/?action=view&reply=PageError");
			}
		}
		
		
		
		
		if(GET('action')=='view'){
			$page = (int) (GET("page") ? GET("page") : 1 );
			if(GET('search'))
			{
				$statement="page where title like '%".GET('q')."%' or heading like '%".GET('q')."%' order by uid desc";
				$this->view->oldData="action=".GET('action')."&q=".GET('q')."search=".GET('q');
			}
			else
			{
				$statement="page order by uid desc";
				$this->view->oldData="action=".GET('action');
			}
			$limit=50;
			$this->view->statement=$statement;
			$this->view->limit=$limit;
			$this->view->page=$page;
			$this->view->PagingNationFetch=$sql->PagingNationFetch($statement,$page,$limit);
			
		}
		elseif(GET('action')=='edit'){
			$this->view->editrow=$sql->fetch("page",array("uid"=>GET('editUid')));
			
			$this->view->Subcategory=$sql->fetchAll("category",array("parent"=>$this->view->editrow['category']));
		}
		
		
		if(GET('reply'))
		{
			$this->view->noti=msg(GET('reply'));
		}
		
		         if (checkPermission('premission_page')) {
            $this->view->render('page');
        } else {
            $this->view->render('permission_denied');
        }
      
       
    }

}

